package com.example.boot.rabbitmq.producer.rest;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.boot.rabbitmq.common.bean.MessageBuilder;
import com.example.boot.rabbitmq.common.consant.MqConstant;
import com.example.boot.rabbitmq.common.entity.Student;
import com.example.boot.rabbitmq.producer.config.RabbitProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author root
 * @date 2021-03-04 16:17
 */
@RestController
@RequiredArgsConstructor
public class IndexRest {


    private final RabbitTemplate rabbitTemplate;
    private final RabbitProperties rabbitProperties;


//    @GetMapping("index")
//    public String index() {
//
//        rabbitTemplate.convertAndSend(MqConstant.EXCHANGE, MqConstant.ROUTING_KEY, "hello rabbit");
//        return "ok";
//    }

    @GetMapping("pro")
    public String pro() {

        return rabbitProperties.getExchangeDlx();
    }


    @GetMapping("indexStudent")
    public String indexStudent() {

        rabbitTemplate.convertAndSend(MqConstant.EXCHANGE, MqConstant.ROUTING_KEY,
                JSONObject.toJSONString(Student.builder().id(1).name("测试").age(11).build()));
        return "ok";
    }


    @GetMapping("indexStudent1")
    public String indexStudent1() {
        MessageBuilder builder = new MessageBuilder();
        MessageBuilder message = builder.withTopic(MqConstant.QUEUE).withAttributes(BeanUtil.beanToMap(Student.builder().age(11).name("小华").id(1).build()))
                .withRoutingKey(MqConstant.ROUTING_KEY);
        rabbitTemplate.convertAndSend(MqConstant.EXCHANGE, MqConstant.ROUTING_KEY,
                message.getAttributes().toString());
        return "ok";
    }
}
